package com.scc.mapper;

import com.scc.pojo.*;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.annotations.Param;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;

@Mapper
public interface UserMapper {
    //员工登录：在数据库中搜索传入过来的员工信息
    @Select("select * from emp where account = #{account} and password = #{password}")
    Emp login(Emp emp);

    //修改员工的登录状态
    @Update("update emp set status = '在线' where account=#{account} and id = #{id}")
    void modifyEmpLoginStatus(Emp emp);

    //根据员工账号获取图片url
    @Select("select * from emp where account = #{account}")
    Emp getEmpImageByAccount(String account);

    //修改员工图片路径
    @Update("update emp set image = #{url} where account = #{account}")
    void modifyEmpImage(String url, String account);

    //员工查询或者获取自己的考勤信息
    List<Attendance> getEmpAttendanceMsgByAccount(@Param("account") String account, @Param("empAttendanceStatus") Integer empAttendanceStatus, @Param("startDate") LocalDate startDate, @Param("endDate") LocalDate endDate);

    //修改员工的考勤状态
    @Update("update attendance set empAttendanceStatus = #{empAttendanceStatus} ,attendance.empAttendanceTime = #{empAttendanceTime} where id = #{id}")
    void modifyEmpAttendanceStatus(Attendance attendance);

    //员工请假提交申请表单(添加操作)
    @Insert("insert into `leave` (name,gender,account,leaveReason,leaveType,leaveDate,leaveDays) value " +
            "(#{name},#{gender},#{account},#{leaveReason},#{leaveType},#{leaveDate},#{leaveDays})")
    void empApplyLeave(Leave leave);

    //员工请假信息表的获取
    List<Leave> getEmpApplyLeave(@Param("account") String account, @Param("approveStatus") Integer approveStatus, @Param("leaveType") String leaveType, @Param("startDate") LocalDate startDate, @Param("endDate") LocalDate endDate);

    //员工修改个人信息
    void modifyEmpMegByAccount(@Param("emp") Emp emp, @Param("id") Integer id);

    //员工离职申请,员工离职信息表添加员工离职信息
    @Insert("insert into resign (name,gender,account,resignDocument,applyDate) value " +
            "(#{name},#{gender},#{account},#{resignDocument},#{applyDate})")
    void empResignApply(Resign emp);

    //员工离职申请表信息的获取或者查询
    List<Resign> getEmpResignMsgByAccount(@Param("account") String account, @Param("approveStatus") Integer approveStatus, @Param("startDate") LocalDate startDate, @Param("endDate") LocalDate endDate);

    //员工获取或者查询培训信息
    List<TrainingActivity> getTrainingActivityMsg(@Param("page") Integer page, @Param("pageSize") Integer pageSize, @Param("trainingName") String trainingName, @Param("activityStatus") Integer activityStatus, @Param("start") LocalDate start, @Param("end") LocalDate end);

    //查询员工培训活动报名表，确定该员工是否报名对应id的培训活动
    EmpTrainingActivity selectEmpTrainingAct(@Param("id") int id, @Param("account") String account);

    //员工培训活动报名信息添加
    @Insert("insert into emptrainingactivity (id,trainingName,name,gender,account,registrationTime) value " +
            "(#{id},#{trainingName},#{name},#{gender},#{account},#{registrationTime})")
    void empRegistrationActivity(EmpTrainingActivity empTrainingActivity);

    //获取员工培训活动表该员工的信息根据id,账号，培训活动名称
    @Select("select * from emptrainingactivity where id = #{id} and account = #{account} and trainingName = #{trainingName}")
    EmpTrainingActivity getEmpTrainingActivityMsg(@Param("id") Integer id, @Param("account") String account, @Param("trainingName") String trainingName);

    //对员工活动报名表的删除根据id,账号
    @Delete("delete from emptrainingactivity where id = #{id} and account = #{account}")
    void empDeleteTrainingActivityMsg(Integer id, String account);

    //修改培训活动信息表的实际报名人数进行加一操作
    @Update("update trainingactivity set actualAttendance = actualAttendance + 1 where id = #{id}")
    void addTrainingActivityActualAttendance(Integer id);

    //修改培训活动信息表的实际报名人数进行减一操作
    @Update("update trainingactivity set actualAttendance = actualAttendance - 1 where id = #{id}")
    void reduceTrainingActivityActualAttendance(Integer id);

    //员工修改密码
    @Update("update emp set password = #{password} where account = #{account}")
    void modifyPassword(Emp emp);

    //员工获取或者查找团建活动信息表
    List<TeamBuildingActivities> getTeamBuildingActivities(@Param("teamBuildingName") String teamBuildingName, @Param("activityStatus") Integer activityStatus, @Param("start") LocalDate start, @Param("end") LocalDate end);

    //用于查找该员工是否报名该团建活动
    @Select("select * from empteambuildingactivityregistration where id = #{id} and account =#{account}")
    EmpTeamBuildingActivityRegistration getRegistrationMsg(int id, String account);

    //添加员工团建活动报名表操作
    @Insert("insert into empteambuildingactivityregistration (id, teamBuildingName, name, gender, account, registrationTime) value " +
            "(#{id},#{teamBuildingName},#{name},#{gender},#{account},#{registrationTime})")
    void addEmpTeamBuildingActMsg(EmpTeamBuildingActivityRegistration teamBuildingActivityMsg);

    //员工报名成功之后团建活动信息表的实际参加人数要加1
    @Update("update teambuildingactivities set actualAttendance = actualAttendance + 1 where id = #{id}")
    void addActualAttendanceNumber(Integer id);

    //获取团建活动的详细对象更具账号id
    @Select("select * from empteambuildingactivityregistration where id = #{id} and account = #{account}")
    EmpTeamBuildingActivityRegistration getEmpTeamBuildingActMsg(Integer id, String account);

    //员工活动报名表删除操作
    @Delete("delete from empteambuildingactivityregistration where account =#{account} and id= #{id}")
    void empDeleteTeamBuildingActivity(Integer id, String account);

    //员工取消活动报名之后，实际参加人数要减一
    @Update("update teambuildingactivities set actualAttendance = actualAttendance - 1 where id = #{id}")
    void reduceTeamBuildingAct(Integer id);

    ///考勤表账号姓名修改操作
    @Update("update attendance set account = #{account},name = #{name} where account = #{oldAccount}")
    void modifyAttendanceByAccount(@Param("account") String account, @Param("name") String name, @Param("oldAccount") String oldAccount);

    //获取旧的员工信息
    @Select("select * from emp where id = #{id}")
    Emp getOldEmpMsg(Integer id);

    //员工请假表改变根据旧的账号
    @Update("update `leave` set account = #{account},name = #{name} where account = #{oldAccount}")
    void modifyLeaveByAccount(@Param("account") String account, @Param("name") String name, @Param("oldAccount") String oldAccount);

    //员工离职表改变根据旧的账号
    @Update("update resign set account = #{account},name = #{name} where account = #{oldAccount}")
    void modifyResignByAccount(@Param("account") String account, @Param("name") String name, @Param("oldAccount") String oldAccount);

    //员工团建活动报名表要改变根据旧的账号
    @Update("update empteambuildingactivityregistration set account = #{account} , name = #{name} where account = #{oldEmpAccount}")
    void modifyTeamBuildingActByAccount(String account, String name, String oldEmpAccount);

    //员工培训活动报名表要改变根据旧的账号
    @Update("update emptrainingactivity set account = #{account} , name = #{name} where account = #{oldEmpAccount}")
    void modifyTrainingActByAccount(String account, String name, String oldEmpAccount);

    //员工获取自己的工资信息
    List<EmpSalary> selectEmpSalaryByAccount(@Param("account") String account, @Param("startDate") LocalDate startDate, @Param("endDate") LocalDate endDate);

    //修该员工工资信息
    @Update("update empsalary set account = #{account} where account = #{oldAccount} ")
    void modifyEmpSalaryByAccount(String account, String oldAccount);

    //根据账号获取员工上限时间
    @Select("select onlineStartTime from emponline where account =#{account}")
    LocalDateTime selectOnlineStartTimeByAccount(String account);

    //获取姓名
    @Select("select name from emp where account = #{account}")
    String getNameByAccount(String account);

    ////员工登录下线之后，记录员工本次在线信息
    @Insert("insert into emponline (name, account, loginDate, onlineStartTime, onlineEndTime, totalOnlineTime, ipAddress) value" +
            " (#{name},#{account},#{loginDate},#{onlineStartTime},#{onlineEndTime},#{totalOnlineMin},#{ipAddress})")
    void insertEmpEndOnlineMsg(String name, String account, LocalDate loginDate, LocalDateTime onlineEndTime, LocalDateTime onlineStartTime, int totalOnlineMin, String ipAddress);

    //员工在线信息表也要改变根据账号在员工修改信息的时候
    @Update("update emponline set account =#{account},name = #{name} where account = #{oldAccount}")
    void modifyEmpOnlineByAccount(String account, String name, String oldAccount);

    //员工操作日志表也要更改
    @Update("update empoperationlog set account = #{account},name = #{name} where account = #{oldAccount}")
    void modifyEmpOperationLogByAccount(String account, String name, String account1);

    //员工根据账号查询好友
    List<Emp> selectEmpMsgByAccount(@Param("account") String account);

    //添加申请信息
    @Insert("insert into friendship_application (applyId, receiveId, applyTime) value " +
            "(#{applyId},#{receiveId},#{applyTime})")
    void insertApplyFriendMsg(FriendshipApplication friendshipApplication);

    //查看直接是否被申请添加好友
    List<Emp> selectFriendApply(@Param("id") Integer id);

    //根据自己的id查找自己接收的好友申请信息
    @Select("select * from friendship_application where receiveId = #{receiveId}")
    List<FriendshipApplication> selectFriendApplyId(Integer receiveId);

    //根据申请人的id获取全部信息
    @Select("select * from emp where id = #{applyId}")
    Emp getApplyEmpMsg(int applyId);

    //获取自己申请好友添加的好友的id
    @Select("select * from friendship_application where applyId = #{applyId} ")
    List<FriendshipApplication> selectApplyId(Integer applyId);

    //修改好友申请表的状态
    @Update("update friendship_application set applyStatus = #{applyStatus} where applyId = #{applyId} and receiveId = #{receiveId} and applyTime = #{applyTime}")
    void modifyApplyStatus(int applyId, int receiveId, LocalDateTime applyTime, int applyStatus);

    //添加申请人和接收人的好友关系
    @Insert("insert into friend_relationship (empId, friendId, addTime) value " +
            "(#{applyId},#{receiveId},#{applyTime})")
    void insertFriendRelationship(int applyId, int receiveId, LocalDateTime applyTime);

    //获取员工的申请状态
    @Select("select * from friendship_application join friend_relationship on friend_relationship.empId = friendship_application.applyId where empId = #{applyId} ")
    List<FriendshipApplication> selectApplyStatus(Integer empId);

    //查找好友根据好友id
    @Select("select * from friend_relationship where empId = #{empId} and friendId = #{friendId}")
    FriendRelationShip selectFriendById(Integer empId, Integer friendId);

    //员工获取自己的好友列表(多表查询)
    List<Emp> selectFriendMsg(@Param("empId") Integer empId);

    //员工删除好友
    @Delete("delete from friend_relationship where empId = #{applyId} and friendId = #{friendId}")
    void deleteFriendRelation(Integer applyId, Integer friendId);

    //查找申请表状态
    @Select("select applyStatus from friendship_application where applyId = #{applyId} and receiveId = #{receiveId} ")
    List<Integer> selectApplyStatusById(Integer applyId, Integer receiveId);

    //修改在线状态
    @Update("update emp set status = '离线' where account = #{account} ")
    void updateEmpStatus(String account);
}
